<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;

app\assets\AppAsset::register($this);
dmstr\web\AdminLteAsset::register($this);
$this->title = '';

/* @var $this yii\web\View */
/* @var $model app\models\News */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="news-form box box-primary">
    <?php 
        $_action[] = $action;
        if (!$model->isNewRecord ) {
            $_action['id'] = $model->id;
        }
        $form = ActiveForm::begin([
        'action' => $_action,
        // 'method'=>'post',  //提交方法(*可省略默认POST*) 
        'id' => 'form-save', //设置ID属性 
          // 'options' => [ 
          //   'class' => 'form-horizontal', //设置class属性 
          // ], 
          // 'enableAjaxValidation' => true, 
          // 'validationUrl' => 'validate-view', 
    ]); ?>
    <div class="box-body table-responsive">
        <?= $form->field($model, 'parent_id')->dropDownList($data ,['disabled' => '']) ?>

        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
        
        <div class="form-group">
        <label class="control-label" for="language">选择语言</label>
        <div >
            <select id="language" class="form" name="language">
                <?php foreach ($languages as $key => $item): ?>
                    <option value="<?=$key;?>"><?=$item['label'];?></option>
                <?php endforeach;?>
            </select>
            <a title="增加" style="padding: 0 6px;margin-top: -4px;" class="btn btn-info" id="add_lang">
                <i class="ace-icon fa fa-plus bigger-130"></i>
            </a>
        </div>
        </div>
        <style>
            .langs{padding-top: 6px;margin-right: 10px;cursor: pointer;}
        </style>
        <div class="form-group">
        <label class="control-label" for="language">已开启语言</label>
        <div >
                <div class="lang-tpl hide" ><label class="langs"><input type="hidden" disabled name="enable_lang[]"><span></span>&nbsp;<i class="ace-icon fa fa-remove bigger-130"></i></label></div>
                <?php 
                if (isset($lang_records) && !empty($lang_records)) {
                foreach ($lang_records as $item) { ?>
                    <label class="langs"><input type="hidden" name="enable_lang[]" value="<?=$item['language'];?>"><span><?=$languages[$item['language']]['label'];?></span>&nbsp;<i class="ace-icon fa fa-remove bigger-130"></i></label>
                <?php }} ?>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label" for="name">名称(对语言)</label>
            <div >
                <?php foreach ($languages as $key => $item): ?>
                    <input lang="<?=$key;?>" id="name" class="col-xs-3" value="<?=isset($lang_records[$key])?$lang_records[$key]['content']:''?>" type="text" name="real_name[<?=$key?>]">
                <?php endforeach;?>
            </div>
        </div>
        <br>

    </div>
    <div class="box-footer">
        <?= Html::submitinput(Yii::t('app', $model->isNewRecord?'新 建':'修 改'), ['class' => 'btn btn-success btn-flat form-submit']) ?>
    </div>
    <?php ActiveForm::end(); ?>
</div>



<?php //按需注入js, 放在</body>前面
$this->registerJs("
        $(function(){
            $('body').on('beforeSubmit', '#form-save', function (e) { 
                //提交之前禁用select disabled
                $('[disabled]').each(function() {//移除disable,后台方可取值  
                    if (parseInt($(this).val()) != -1) {  
                        $(this).attr('disabled', false);  
                    }  
                }); 

                var form = $(this); 
                //返回错误的表单信息 
                if (form.find('.has-error').length) 
                { 
                  return false; 
                } 
                
                var trans_box_index = layer.msg('loading', {icon: 16, time: 0, shadeClose: true, shade: [0.7, '#000']});
                //表单提交 
                $.ajax({ 
                  url  : form.attr('action'), 
                  type  : 'post', 
                  dataType:'json',
                  data  : form.serialize(), 
                  success: function (response){
                    if(response.ok == 0){ 
                      window.parent.location.reload();
                    } else{
                        layer.alert(response.message, {title: '提示', btn: '', shadeClose: true, shade: [0.7, '#000'], offset: 'auto', time: 2000});
                    }
                  }, 
                }); 
                return false; 
            }).on('submit', function(e){
                 e.preventDefault();
            });
        })

        //添加删除语言
        $('#add_lang').click(function(event) {
            event.preventDefault();
            if ($('.langs input[value=' + $('#language').val() + ']').length > 0) {
                return false;
            }
            var dom = $('.lang-tpl').clone();
            dom.find('label').removeClass('tpl hide').find('input').prop('disabled', false).val($('#language').val());
            dom.find('span').html($('#language option:selected').html());
            $('.lang-tpl').after(dom.html());
        });
        $(document).on('click', '.langs i', function(event) {
            event.preventDefault();
            $(this).closest('label').remove();
        });

        function init_lang(lang){
            $('body [lang]').not('[lang=' + lang + ']').hide();
            $('[lang=' + lang + ']').show();
            // $('.edui-default').not('[id=myEditor_' + lang + ']').parent().hide();
            // $('[id=myEditor_' + lang + ']').parent().show();

        }
        $('#language').change(function(event) {
            init_lang($(this).val());
        });
        $('#language').trigger('change');
    ", \yii\web\View::POS_END);
 ?>

